<template>
  <div style="width:100%;height:100%;" v-loading="loading">
    <el-dialog title="添加类型" :close-on-click-modal="false" :visible.sync="petTypeDialog" width="30%" @close="closeHandler">
      <el-form :model="petType" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="类型编号" prop="typeId">
          <el-input v-model="petType.typeId"></el-input>
        </el-form-item>
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="petType.typeName"  ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="closeHandler()">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {petTypeSaveUrl} from '@/utils/common'
export default {
  props: {
    petTypeDialog: {
      type: Boolean,
      default: false
    },
    popPetType: {
      type: Object,
      default: null
    }
  },
  watch: {
    popPetType() {
      this.petType = this.popPetType;
    }
  },
  data() {
    return {
      loading: false,
      petType: {
      },
      rules: {
        typeId: { required: true, message: "请输入类型编号", trigger: "blur" },
        typeName: { required: true, message: "请输入类型名称", trigger: "blur" },
      }
    }
  },
  mounted() {
  },
  methods: {
  
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.savePetType();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    savePetType() {
      this.loading = true;
      this.axios
        .post(petTypeSaveUrl, this.petType)
        .then(res => {
          if (res.data.status == "SUCCESS") {
            this.loading = false;
            this.$emit("close");
            this.$message.success(res.data.result);
          } else {
            this.loading = false;
            this.$message.error(res.data.result);
          }
        })
        .catch(response => {
          this.loading = false;
          console.log("response - " + response);
          this.$message.error("数据保存异常失败");
        });
    },
    closeHandler: function () {
      this.dialogVisible = false;
      this.user = {};
      this.$emit("close");
    }
  }
};
</script>

<style>
  .el-select .el-input {
    width: 130px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>
